package cn.liziguo.tank.collision;

import cn.liziguo.tank.config.GameConfig;
import cn.liziguo.tank.constants.GameEnum;
import cn.liziguo.tank.script.BulletComponent;
import com.almasb.fxgl.dsl.FXGL;
import com.almasb.fxgl.entity.Entity;
import com.almasb.fxgl.physics.CollisionHandler;

import java.util.List;

/**
 * @author Liziguo
 * @date 2022-07-09 21:26:11
 */
public class BulletBrickCollisionHandler extends CollisionHandler {

    public BulletBrickCollisionHandler() {
        super(GameEnum.BULLET, GameEnum.BRICK);
    }

    @Override
    protected void onCollisionBegin(Entity bullet, Entity b) {
        List<Entity> list = FXGL.getGameWorld().getGroup(GameEnum.GREENS, GameEnum.BRICK, GameEnum.STONE).getEntitiesCopy();
        for (Entity map : list) {
            if (bullet.isColliding(map)) {
                GameEnum type = (GameEnum) map.getType();
                switch (type) {
                    case BRICK -> {
                        map.removeFromWorld();
                    }
                    case GREENS -> {
                        if (bullet.getComponent(BulletComponent.class).level >= GameConfig.DESTROY_GREENS_BULLET_LEVEL) {
                            map.removeFromWorld();
                        }
                    }
                    case STONE -> {
                        if (bullet.getComponent(BulletComponent.class).level >= GameConfig.DESTROY_STONE_BULLET_LEVEL) {
                            map.removeFromWorld();
                        }
                    }
                }
            }
        }
        BulletComponent.onRemove(bullet);
    }
}
